﻿var P5 = function (game) {
  var flag = [0, 0, 0, 0];
  this.create = function () {


    ClosePage(game, '5_0', '5', '5_r', '5_w');

    function Page(game, cbg, mbg, rbg, wbg) {
        var flagAry=[];  //0, 6, 7, 9, 4, 3, 2
        var bg = game.add.sprite(0, 0, mbg);
        bg.anchor.setTo(0, 0);

        bounds = new Phaser.Rectangle(240, 160, 1146, 450);

        var graphics = game.add.graphics(bounds.x, bounds.y);
        graphics.beginFill(0x000077);
        graphics.drawRect(0, 0, bounds.width, bounds.height);
        graphics.visible = false;


        var group = game.add.group();
        var originX = 1130;
        var originY = [];
        var tween;
        var style = {
            font: "19px",
            fill: "#fff",
            boundsAlignH: "center",
            boundsAlignV: "middle",
        };
        var text;
        var drag;
        var rectGroup = game.add.group();
        var xPos = [840, 940, 877, 530, 370, 360, 333];
        var yPos = [];
        var names = ['厚重','酸度','24-30˚C','海拔600m+','15-24˚C','海拔600m-','油脂','香气'];

        var Lwords=['海拔600m+','15-24˚C','酸度','香气'];
        var ltext,rtext;

        var Rwords=[ '海拔600m-','24-30˚C','厚重','油脂'];
        var LW=[], RW=[];


        var Lrect = game.add.sprite(350,160, 'rectbg');
        Lrect.height=160;
        Lrect.width=250;
        var Rrect = game.add.sprite(1020,160, 'rectbg');

        for (var i = 0; i < names.length; i++) {
            flagAry.push('#')
            drag = game.add.sprite(700,150+i*60, 'rect');
            yPos.push(150+i*60);
            text = game.add.text(0, 0, names[i], style);
            text.x=(drag.width-text.width)/2;
            text.y=(drag.height-text.height)/2;
            drag.addChild(text);
            text.font = 'Microsoft YaHei';
            drag.data = i;
            drag.name = names[i];
            drag.inputEnabled = true;
            drag.input.useHandCursor = true;
            drag.input.enableDrag(false, true);
            group.add(drag);
            drag.events.onDragStop.add(fixLocation);
        }


        var styleText = { font: "17px", fill: "#000",
            boundsAlignH: "center", };

        var lgroup=game.add.group();
        var rgroup=game.add.group();
        var tempSprite;

        function showWrod(type) {
            if(type === 'L') {
                lgroup.destroy();
                lgroup=game.add.group();
                for (var i = 0; i < LW.length; i++) {
                    if(i<4) {
                        tempSprite = game.add.sprite(365, 175 + i * 35, 'bluerect');
                        text = game.add.text(10, 0, LW[i], styleText);
                         text.font = 'Microsoft YaHei';
                    }else{
                        tempSprite = game.add.sprite(475, 175 + (i-4) * 35, 'bluerect');
                        text = game.add.text(10, 0, LW[i], styleText);
                         text.font = 'Microsoft YaHei';
                    }
                    text.y=(tempSprite.height-text.height)/2;
                    tempSprite.addChild(text);

                    tempSprite.name = LW[i];
                    tempSprite.type = 'L';
                    tempSprite.inputEnabled = true;
                    tempSprite.input.useHandCursor = true;
                    tempSprite.events.onInputDown.add(wordToOrigin, this);
                    lgroup.add(tempSprite);
                }
            }else{
                rgroup.destroy();
                rgroup=game.add.group();
                for (var i = 0; i < RW.length; i++) {
                    if(i<4) {
                        tempSprite = game.add.sprite(1035, 175 + i * 35, 'bluerect');
                        text = game.add.text(10, 0, RW[i], styleText);
                        text.font = 'Microsoft YaHei';
                    }else{
                        tempSprite = game.add.sprite(1145, 175 + (i-4) * 35, 'bluerect');
                        text = game.add.text(10, 0, RW[i], styleText);
                        text.font = 'Microsoft YaHei';
                    }

                    text.y=(tempSprite.height-text.height)/2;
                    tempSprite.addChild(text);

                    tempSprite.name = RW[i];
                    tempSprite.type = 'R';
                    tempSprite.inputEnabled = true;
                    tempSprite.input.useHandCursor = true;
                    tempSprite.events.onInputDown.add(wordToOrigin, this);
                    rgroup.add(tempSprite);
                }
            }

        }


        game.world.bringToTop(group);

        function wordToOrigin(arg) {
          if(arg.type === 'L'){
              LW=LW.filter(function (item) {
                  return arg.name!=item;
              });
          }else{
              RW=RW.filter(function (item) {
                  return arg.name!=item;
              });
          }

          showWrod(arg.type);
          group.getByName(arg.name).scale.setTo(1,1);
        }

        function isSelect(word){
          var temp=LW.concat(RW);
          var f1=temp.some( function(item){
              return item ===word;
          })
            if(f1){
              return true;
            }else{
              return false;
            }
        };

        function fixLocation(item) {
          
            var bounds = item.getBounds();
            var Lbound=Lrect.getBounds();
            var Rbound=Rrect.getBounds();

            if( Phaser.Rectangle.intersects(bounds, Lbound) && (!isSelect(item.name))){
                LW.push(item.name);
                showWrod('L');
                //item.visible=false;
                item.scale.setTo(0.0,0.0)

            }else if(Phaser.Rectangle.intersects(bounds, Rbound) &&  (!isSelect(item.name))){
                RW.push(item.name);
                showWrod('R');
                item.scale.setTo(0.0,0.0)
            }else{

            }
            tween = game.add.tween(item).to({x:700 , y: yPos[item.data]}, 50, Phaser.Easing.Linear.None, true);

        }


        game.add.button(1100, 630, imgs['prev'], onPrev, this, 2, 1, 0);
        game.add.button(1250, 630, imgs['submit'], onSubmit, this, 2, 1, 0);
        function onPrev() {
            game.goState('back');
            group.destroy();
        }
      function onSubmit(arg) {
          if(LW.sort().join('#') == Lwords.sort().join('#')
                && RW.sort().join('#') == Rwords.sort().join('#')
          ){
          RightPage(game, cbg, mbg, rbg, wbg);
        } else {
          WrongPage(game, cbg, mbg, rbg, wbg);
        }
      }

    }

      function ClosePage(game, cbg, mbg, rbg, wbg) {
          var container = game.add.sprite(0, 0, '');
          var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
          container.addChild(close);

          function onClose() {
              container.destroy();
              Page(game, cbg, mbg, rbg, wbg);
          }
      }

      function WrongPage(game, cbg, mbg, rbg, wbg) {
          var container = game.add.sprite(0, 0, '');

          var bg = game.add.image(0, 0, wbg);
          container.addChild(bg);

          var next = game.add.button(1250, 630, imgs['retry'], onClick, this, 2, 1, 0);
          container.addChild(next);
          function onClick() {
              container.destroy();
              Page(game, cbg, mbg, rbg, wbg);
          }
      }

      function RightPage(game, cbg, mbg, rbg, wbg) {
          var container = game.add.sprite(0, 0, '');
          var bg = game.add.image(0, 0, rbg);
          container.addChild(bg);
          var next = game.add.button(1250, 630, imgs['next'], onClick, game, 2, 1, 0);
          container.addChild(next);

          function onClick() {
              game.goState('next');
          }
      }
  }


}

